<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/11/8
 * Time: 13:36
 */

namespace app\index\controller;
use app\index\base_controller\BaseController;
use app\index\table\LiquidTable;
use think\Db;

class Liquid extends BaseController{

    /**
     * page--基本信息
     */
    public function liquidList(){
        return view('liquid_list');
    }

    /**
     * action--获取信息
     */
    public function getLiquidList(){
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'LIQUID_NO desc';
        }

       $condition = LiquidTable::getSessionConditions();

        try{

            $listSql = LiquidTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = LiquidTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page--新增页面
     */
    public function addPage(){
        $config = LiquidTable::getConfigAdd();
        $this->assign('config',$config);
        $this->assign('data',null);
        $this->view->engine->layout('layout_popup');
        return view('liquid_add');
    }

    /**
     * 新增
     */
    public function add() {
        $data = input();
        try{
            LiquidTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page--编辑页面
     */
    public function editPage(){
        $liquidNo = input('liquidNo');
        $data = LiquidTable::get($liquidNo);
        if(empty($data)){
            return '查询流动资产信息失败';
        }
        $config = LiquidTable::getConfigEdit();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('liquid_edit');
    }

    /**
     * 编辑
     */
    public function edit() {
        $liquidNo = input('LIQUID_NO');
        $data = input();
        try{
            LiquidTable::update(['LIQUID_NO'=>$liquidNo],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * action 批量删除--教育资源产出指标
     */
    public function deleteAll(){
        $list = input('list');
        Db::startTrans();
        try{
            foreach ($list as $liquidNo){
                LiquidTable::delete(['LIQUID_NO'=>$liquidNo]);
            }
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }


    /**
     * page--高级查询
     */
    public function searchPage(){
        $this->view->engine->layout('layout_popup');
        return view('liquid_search');
    }

    /**
     * page--变动信息
     */
    public function liquidAlterList(){
        return view('liquid_alter_list');
    }

    /**
     * action--获取变动信息
     */
    public function getLiquidAlterList(){

    }

    /**
     * 打印
     */
    public function printPage() {
        $liquidNo = input('liquidNo');
        $data = LiquidTable::get($liquidNo);
        if(empty($data)){
            return '查询流动资产信息失败';
        }
        $config = LiquidTable::getConfigPrint();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('liquid_print');
    }

    public function addConditionSession() {
        $condition = input();
        Login::setListConditionLiquid($condition);
        return json_encode(['code'=>'200']);
    }

    public function importPage() {
        $this->view->engine->layout('layout_popup');
        return view('import');
    }

    public function import() {
        $inputData = input('data');
        $postData = base64_decode($inputData);
        $list = json_decode($postData,true);
        $start = input('start');

        Db::startTrans();
        $fieldList = LiquidTable::LIQUID_FIELD_LIST;
        $fieldCount = count($fieldList);
        foreach ($list as $key => $row) {
            $data = [];

            // 字段赋值
            $i = 0;
            foreach ($row as $key1=>$value) {
                $data[$fieldList[$i]] = $value;
                $i++;
                if ($i>=$fieldCount) {
                    break;
                }
            }

            try{
                LiquidTable::addImport($data);
            }
            catch (Exception $e){
                Db::rollback();
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错",'data'=>$e->getMessage()]);
            }
        }

        Db::commit();
        return json_encode(['code'=>'200','msg'=>'导入成功']);
    }

    /**
     * page 流动资产导出页面
     */
    public function exportExcelPage() {
        $condition = LiquidTable::getSessionConditions();

        try{
            $totalSql = LiquidTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);

            $arr = $this->getDownLoadList($total);
            $this->assign('downList',$arr);

            $this->view->engine->layout('layout_popup');
            return view('export_excel');
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page 导出Excel--教育资源产出指标
     */
    public function exportExcel(){
        $sortStr = 'LIQUID_NO desc';
        $start = input('start');
        $startL = $start-1;
        $end = input('end');
        $endL = $end-$start+1;

        $condition = LiquidTable::getSessionConditions();

        try{
            $listSql = LiquidTable::table()->where($condition)->limit($startL,$endL)->order($sortStr)->fetchSql()->select();
            $list = Db::query($listSql);
            LiquidTable::exportExcel($list,"流动资产$start-$end");

        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }


}